package cn.com.pajx.pajx_visitor_v3.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.rcw.swiperefreshrecyclerview.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.pajx.pajx_visitor_v3.R;
import cn.com.pajx.pajx_visitor_v3.adapter.ChooseIntervieweeAdapter;
import cn.com.pajx.pajx_visitor_v3.api.Api;
import cn.com.pajx.pajx_visitor_v3.base.BaseRecyclerViewFragment;
import cn.com.pajx.pajx_visitor_v3.bean.InterviewPageBean;
import cn.com.pajx.pajx_visitor_v3.bean.IntervieweeBean;

/**
 * 学择拜访对象页面
 * create an instance of this fragment.
 */
@SuppressLint("NonConstantResourceId")
public class ChooseIntervieweeFragment extends BaseRecyclerViewFragment<IntervieweeBean> {

    @BindView(R.id.view_search)
    EditText viewSearch;

    private ChooseIntervieweeAdapter userAdapter;
    private final List<IntervieweeBean> intervieweeBeans = new ArrayList<>();
    private OnFunctionListener onFunctionListener;
    private int totalPage = 0;

    public ChooseIntervieweeFragment() {
    }

    public static ChooseIntervieweeFragment newInstance() {
        ChooseIntervieweeFragment fragment = new ChooseIntervieweeFragment();
        return fragment;
    }

    @Override
    protected void beforeInitHead() {
        super.beforeInitHead();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_choose_interviewee;
    }

    @Override
    protected String getApiUrl(boolean isRefresh) {
        if (isRefresh) intervieweeBeans.clear();
        paramsMap.put("keyword", viewSearch.getText().toString().trim());
        return Api.CHOOSE_INTERVIEWEE;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        super.initView(view, savedInstanceState);
        userAdapter.setOnFunctionListener(bean -> {
            if (onFunctionListener != null) {
                onFunctionListener.onChoose(bean);
            }
            dismiss();
        });
    }

    @Override
    protected BaseAdapter getAdapter() {
        return userAdapter = new ChooseIntervieweeAdapter(mContext, R.layout.choose_interviewee_item, intervieweeBeans);
    }

    @Override
    protected boolean getLoadMoreEnable() {
        return true;
    }

    @Override
    protected void showData(String data, String tag) {
        InterviewPageBean interviewPageBean = new Gson().fromJson(data, InterviewPageBean.class);
        if (interviewPageBean != null) {
            totalPage = interviewPageBean.getPages();
            intervieweeBeans.addAll(interviewPageBean.getRecords() == null ? new ArrayList<>() : interviewPageBean.getRecords());
            showEmptyStatus(intervieweeBeans);
            helper.setTextColor(mContext);
        }
    }

    @Override
    public void onFailed(Throwable throwable, String tag) {
        super.onFailed(throwable, tag);
        helper.setTextColor(mContext);
    }

    @Override
    protected int getTotalPage() {
        return totalPage;
    }

    @Override
    protected String loadEmptyDes() {
        return "未查询到教师";
    }

    @SuppressLint("NotifyDataSetChanged")
    @OnClick({R.id.tv_search, R.id.iv_close})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.iv_close) {
            dismiss();
        } else {
            hideSoftInput();
            onRefresh();
        }
    }

    public interface OnFunctionListener {
        void onChoose(IntervieweeBean bean);
    }

    public void setOnFunctionListener(OnFunctionListener onFunctionListener) {
        this.onFunctionListener = onFunctionListener;
    }
}